<?php
namespace app\api\controller;
use app\common\controller\CommonApi;
use Pay\Pay;
/**
 * 会员等级相关
 */
class Grade extends CommonApi{

    //会员升级
    public function upGrade(){

        $validate = new \think\validate([
            'id' => 'require|in:1,2,3',
            'pay_type' => 'require|in:0,1,2'
        ],[
            'id.require' => '请选择升级方式',
            'id.in' => '升级方式错误',
            'pay_type.require' => '请选择支付方式',
            'pay_type.in' => '支付方式有误'
        ]);
        if($validate->check($this->post) === false){
            $this->apiReturn(0,$validate->getError());
        }

        $res = db('grade')->where('id',$this->post['id'])->find();
        if(is_null($res)){
            $this->apiReturn(0,'升级方式不存在');
        }

        $order_no = date('YmdHis').rand(10000,99999); //订单编号

        //余额支付
        if($this->post['pay_type'] == 0){
            if($this->user_info['balance'] < $res['price']){
                $this->apiReturn(0,'账户余额不足');
            }

            //以前是会员
            if($this->user_info['grade'] == 1){
                $grade_end_time = strtotime($this->user_info['grade_end_time']);
            }else{
                $grade_end_time = strtotime(date('Ymd'));
            }

            switch($this->post['id']){
                case 1: $grade_end_time = strtotime('+1 month',$grade_end_time); break;  //包月
                case 2: $grade_end_time = strtotime('+6 month',$grade_end_time); break; //半年
                case 3: $grade_end_time = strtotime('+1 years',$grade_end_time); break; //包年
            }

            db()->startTrans();
            //修改会员信息
            if(!db('user')->where('id',UID)->update([
                'balance' => ['dec',$res['price']],
                'grade_end_time' => $grade_end_time,
                'grade' => 1
            ])){
                db()->rollback();
                $this->apiReturn(0,'会员信息更新失败');
            }

            $insert = db('upgrade')->insertGetId([
                'user_id' => UID,
                'grade_type' => $this->post['id'],
                'price' => $res['price'],
                'order_no' => $order_no,
                'pay_time' => time()
            ]);

            //购买记录
            if(!$insert){
                db()->rollback();
                $this->apiReturn(0,'记录添加失败');
            }

            if(!db('bill')->insert([
                'user_id' => UID,
                'type' => 11,
                'num' => $res['price'],
                'foreign_key' => $insert,
                'add_time' => time()
            ])){
                db()->rollback();
                $this->apiReturn(0,'交易明细添加失败');
            }

            if(!db('information')->insert([
                'user_id' => UID ,
                'type' => 11 ,
                'content' => '购买会员-支付成功',
                'add_time' => time()
            ])){
                db()->rollback();
                $this->apiReturn(0,'消息通知添加失败');
            }
            db()->commit();
            $this->apiReturn(1,'会员购买成功');
        }
        //微信支付
        if($this->post['pay_type'] == 1){
            $options = [
                'out_trade_no' => $order_no,
                'total_fee' => $res['price']*100,
                'body' => '购买会员',
                'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
                'attach' => serialize(['attach' => 6 , 'type' => $this->post['id'],'user_id' => UID])
            ];

            $pay = new Pay(config('pay'));

            try{
                $result = $pay->driver('wechat')->gateway('app')->apply($options);
                $this->apiReturn(1,'微信支付信息',$result);
            } catch (Exception $e){
                $this->apiReturn(0,$e->getMessage());

            }
        }
        //支付宝支付
        if($this->post['pay_type'] == 2){
            $options = [
                'out_trade_no' => $order_no,
                'total_amount' => $res['price'],
                'subject' => '购买会员',
                'passback_params' => UrlEncode(serialize(['passback_params' => 1 , 'type' => $this->post['id'],'user_id' => UID]))
            ];
            $pay = new Pay(config('pay'));
            try{
                $result = $pay->driver('alipay')->gateway('app')->apply($options);
                $this->apiReturn(1,'支付宝支付信息',$result);
            } catch (Exception $e){
                $this->apiReturn(0,$e->getMessage());

            }
        }
    }
    
}
